<HTML>

<HEAD>
        <META NAME="keywords" CONTENT="electronic commerce, ecommerce, ebusiness, e-business, e-commerce, enterprise software, net economy, Netscape, Sun Microsystems, Sun-Netscape Alliance, iPlanet, internet software">
        <META NAME="description" CONTENT="The Sun-Netscape Alliance, a strategic alliance formed by America Online and Sun Microsystems, Inc., is delivering iPlanet e-commerce software and enterprise solutions that enable companies to compete successfully in the Net Economy">
	<META HTTP-EQUIV="Content-Type" content="text/html; charset=ISO-8859-1">
	<META NAME="TEMPLATEBASE" CONTENT="Authored in FrameMaker. Converted to HTML in WebWorks Publisher 2000. manual wdt 1.5">
	<META NAME="LASTUPDATED" CONTENT="09/28/00 13:18:02">
	<TITLE> Core JavaScript Guide 1.5: 7 Working with Objects</TITLE>
</HEAD>




<BODY TEXT="#000000" LINK="#666699" VLINK="#666699" ALINK="#333366" BGCOLOR="#FFFFFF">

<MAINCONTENT>

<NAVIGATIONCONTENT>
   <TABLE WIDTH="100%" BORDER="0" CELLSPACING="4">
      <TR>
         <TD VALIGN="TOP">
            <b><a href="fcns.html">
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2" COLOR="#993300">Previous</a></FONT>&nbsp;&nbsp;&nbsp;&nbsp;</b>
<a href="contents.html">
<b><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2" COLOR="#993300">Contents</a></FONT></b>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="ix.html"><b><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2" COLOR="#993300">Index</a></FONT></b>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="obj2.html">
<b>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2" COLOR="#993300">
Next
</a>
</FONT>
</b>&nbsp;&nbsp;&nbsp;&nbsp;
         </TD>
     </TR>

      <TR>
         <TD VALIGN="TOP" ALIGN="RIGHT" BGCOLOR="#CCCCCC">
            <FONT FACE="Arial, Helvetica, sans-serif" COLOR="#000000" SIZE="2"><B>
             Core JavaScript Guide 1.5</B></FONT>
         </TD>
      </TR>
      <TR>
   </TABLE>
   <BR>
</NAVIGATIONCONTENT>

<BOOKCONTENT>
   <blockquote>
<BR>
<BR>

      <A NAME="1008300"> </A><A NAME=""> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="4" COLOR="#666666"><B>
<zchapnumber>Chapter 7 </zchapnumber>
&nbsp;
</B>
</FONT>


<A NAME="1008302"> </A>
<A NAME="Working with Objects"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="4" COLOR="#000000">
<B>
<zchaptitle>Working with Objects</zchaptitle>
</B>
</FONT>
<BR>
<BR>
<BR>

<A NAME="1008307"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>JavaScript is designed on a simple object-based paradigm. An object is a construct with properties that are JavaScript variables or other objects. An object also has functions associated with it that are known as the object's <I>methods</I>. In addition to objects that are predefined in the Navigator client and the server, you can define your own objects.</zparagraph>
</FONT>
<P>

<A NAME="1008309"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>This chapter describes how to use objects, properties, functions, and methods, and how to create your own objects. </zparagraph>
</FONT>
<P>

<A NAME="1010098"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>This chapter contains the following sections:</zparagraph>
</FONT>
<P>
<ul>
<A NAME="1010099"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1><A HREF="obj.html#1008312"  >Objects and Properties</A></zsmartlist1>
</FONT>
<P><A NAME="1010100"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1><A HREF="obj.html#1008327"  >Creating New Objects</A></zsmartlist1>
</FONT>
<P><A NAME="1010136"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1><A HREF="obj.html#1010352"  >Predefined Core Objects</A></zsmartlist1>
</FONT>
<P></ul>

<BR>
<BR>

<A NAME="1008312"> </A>
<A NAME="Objects and Properties"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="4">
<zhead1>Objects and Properties</zhead1>
<HR SIZE="2" NOSHADE>
</FONT>
<BR>


<A NAME="1008313"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>A JavaScript object has properties associated with it. You access the properties of an object with a simple notation:</zparagraph>
</FONT>
<P>
<A NAME="1008314"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline><I>objectName</I>.<I>propertyName
</I></zcodeline>
</FONT>
<P>
<A NAME="1008316"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>Both the object name and property name are case sensitive. You define a property by assigning it a value. For example, suppose there is an object named <FONT FACE="courier, couriernew, monospaced size">myCar</FONT> (for now, just assume the object already exists). You can give it properties named <FONT FACE="courier, couriernew, monospaced size">make</FONT>, <FONT FACE="courier, couriernew, monospaced size">model</FONT>, and <FONT FACE="courier, couriernew, monospaced size">year</FONT> as follows:</zparagraph>
</FONT>
<P>
<A NAME="1008317"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>myCar.make = "Ford";<br>
myCar.model = "Mustang";<br>
myCar.year = 1969;
</zcodeline>
</FONT>
<P>
<A NAME="1008319"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>An array is an ordered set of values associated with a single variable name. Properties and arrays in JavaScript are intimately related; in fact, they are different interfaces to the same data structure. So, for example, you could access the properties of the <FONT FACE="courier, couriernew, monospaced size">myCar</FONT> object as follows:</zparagraph>
</FONT>
<P>
<A NAME="1008320"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>myCar["make"] = "Ford"<br>
myCar["model"] = "Mustang"<br>
myCar["year"] = 1967
</zcodeline>
</FONT>
<P>
<A NAME="1008321"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>This type of array is known as an <I>associative array</I>, because each index element is also associated with a string value. To illustrate how this works, the following function displays the properties of the object when you pass the object and the object's name as arguments to the function:</zparagraph>
</FONT>
<P>
<A NAME="1008322"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>function show_props(obj, obj_name) {<br>
&nbsp;&nbsp;&nbsp;var result = "";<br>
&nbsp;&nbsp;&nbsp;for (var i in obj)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result += obj_name + "." + i + " = " + obj[i] + "\n";<br>
&nbsp;&nbsp;&nbsp;return result<br>
}
</zcodeline>
</FONT>
<P>
<A NAME="1008323"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>So, the function call <FONT FACE="courier, couriernew, monospaced size">show_props(myCar, "myCar")</FONT> would return the following:</zparagraph>
</FONT>
<P>
<A NAME="1008324"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>myCar.make = Ford<br>
myCar.model = Mustang<br>
myCar.year = 1967
</zcodeline>
</FONT>
<P>
<BR>
<BR>

<A NAME="1008327"> </A>
<A NAME="Creating New Objects"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="4">
<zhead1>Creating New Objects</zhead1>
<HR SIZE="2" NOSHADE>
</FONT>
<BR>


<A NAME="1010155"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>JavaScript has a number of predefined objects. In addition, you can create your own objects. In JavaScript&nbsp;1.2 and later, you can create an object using an object initializer. Alternatively, you can first create a constructor function and then instantiate an object using that function and the <FONT FACE="courier, couriernew, monospaced size">new</FONT> operator.</zparagraph>
</FONT>
<P>
<P>
<BR>
<A NAME="1008330"> </A>
<A NAME="Using Object Initializers"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="4">
<zhead2>Using Object Initializers</zhead2>
</FONT>
<BR>
<BR>



<A NAME="1016607"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>In addition to creating objects using a constructor function, you can create objects using an object initializer. Using object initializers is sometimes referred to as creating objects with literal notation. "Object initializer" is consistent with the terminology used by C++.</zparagraph>
</FONT>
<P>

<A NAME="1016611"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The syntax for an object using an object initializer is:</zparagraph>
</FONT>
<P>
<A NAME="1016612"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>objectName = {property1:value1, property2:value2,..., property<I>N</I>:value<I>N</I>}
</zcodeline>
</FONT>
<P>
<A NAME="1008340"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>where <FONT FACE="courier, couriernew, monospaced size">objectName</FONT> is the name of the new object, each <FONT FACE="courier, couriernew, monospaced size">property</FONT><FONT FACE="courier, couriernew, monospaced size"><I>I</I></font> is an identifier (either a name, a number, or a string literal), and each <FONT FACE="courier, couriernew, monospaced size">value</FONT><FONT FACE="courier, couriernew, monospaced size"><I>I</I></font> is an expression whose value is assigned to the <FONT FACE="courier, couriernew, monospaced size">property</FONT><FONT FACE="courier, couriernew, monospaced size"><I>I</I></font>. The <FONT FACE="courier, couriernew, monospaced size">objectName</FONT> and assignment is optional. If you do not need to refer to this object elsewhere, you do not need to assign it to a variable.</zparagraph>
</FONT>
<P>

<A NAME="1008341"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>If an object is created with an object initializer in a top-level script, JavaScript interprets the object each time it evaluates the expression containing the object literal. In addition, an initializer used in a function is created each time the function is called.</zparagraph>
</FONT>
<P>

<A NAME="1016636"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The following statement creates an object and assigns it to the variable <FONT FACE="courier, couriernew, monospaced size">x</FONT> if and only if the expression <FONT FACE="courier, couriernew, monospaced size">cond</FONT> is true.</zparagraph>
</FONT>
<P>
<A NAME="1010172"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>if (cond) x = {hi:"there"}
</zcodeline>
</FONT>
<P>
<A NAME="1008345"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The following example creates <FONT FACE="courier, couriernew, monospaced size">myHonda</FONT> with three properties. Note that the <FONT FACE="courier, couriernew, monospaced size">engine</FONT> property is also an object with its own properties. </zparagraph>
</FONT>
<P>
<A NAME="1008346"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>myHonda = {color:"red",wheels:4,engine:{cylinders:4,size:2.2}}
</zcodeline>
</FONT>
<P>
<A NAME="1016639"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>You can also use object initializers to create arrays. See <A HREF="ident.html#1011655"  >"Array Literals" on page&nbsp;28</A>.</zparagraph>
</FONT>
<P>
<P>
<A NAME="1016568"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>
<zheadrunin>JavaScript 1.1 and earlier. </zheadrunin>
</B>
</FONT>

<A NAME="1016569"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>You cannot use object initializers. You can create objects only using their constructor functions or using a function supplied by some other object for that purpose. See <A HREF="obj.html#1008361"  >Using a Constructor Function</A>.</zparagraph>
</FONT>
<P>
<P>
<BR>
<A NAME="1008361"> </A>
<A NAME="Using a Constructor Function"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="4">
<zhead2>Using a Constructor Function</zhead2>
</FONT>
<BR>
<BR>



<A NAME="1008362"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>Alternatively, you can create an object with these two steps:</zparagraph>
</FONT>
<P>
<ol type="1">
<A NAME="1008363"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li value="1"></B>
<zsmartlist1>Define the object type by writing a constructor function.</zsmartlist1>
</FONT>
<P><A NAME="1008365"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li value="2"></B>
<zsmartlist1>Create an instance of the object with <FONT FACE="courier, couriernew, monospaced size">new</FONT>.</zsmartlist1>
</FONT>
<P></ol>

<A NAME="1008366"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>To define an object type, create a function for the object type that specifies its name, properties, and methods. For example, suppose you want to create an object type for cars. You want this type of object to be called <FONT FACE="courier, couriernew, monospaced size">car</FONT>, and you want it to have properties for make, model, year, and color. To do this, you would write the following function:</zparagraph>
</FONT>
<P>
<A NAME="1008367"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>function car(make, model, year) {<br>
&nbsp;&nbsp;&nbsp;this.make = make;<br>
&nbsp;&nbsp;&nbsp;this.model = model;<br>
&nbsp;&nbsp;&nbsp;this.year = year;<br>
}
</zcodeline>
</FONT>
<P>
<A NAME="1008369"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>Notice the use of <FONT FACE="courier, couriernew, monospaced size">this</FONT> to assign values to the object's properties based on the values passed to the function.</zparagraph>
</FONT>
<P>

<A NAME="1008370"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>Now you can create an object called <FONT FACE="courier, couriernew, monospaced size">mycar</FONT> as follows:</zparagraph>
</FONT>
<P>
<A NAME="1008371"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>mycar = new car("Eagle", "Talon TSi", 1993);
</zcodeline>
</FONT>
<P>
<A NAME="1008372"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>This statement creates <FONT FACE="courier, couriernew, monospaced size">mycar</FONT> and assigns it the specified values for its properties. Then the value of <FONT FACE="courier, couriernew, monospaced size">mycar.make</FONT> is the string "Eagle", <FONT FACE="courier, couriernew, monospaced size">mycar.year</FONT> is the integer 1993, and so on.</zparagraph>
</FONT>
<P>

<A NAME="1008373"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>You can create any number of <FONT FACE="courier, couriernew, monospaced size">car</FONT> objects by calls to <FONT FACE="courier, couriernew, monospaced size">new</FONT>. For example,</zparagraph>
</FONT>
<P>
<A NAME="1008374"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>kenscar = new car("Nissan", "300ZX", 1992);<br>
vpgscar = new car("Mazda", "Miata", 1990);
</zcodeline>
</FONT>
<P>
<A NAME="1018985"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>An object can have a property that is itself another object. For example, suppose you define an object called <FONT FACE="courier, couriernew, monospaced size">person</FONT> as follows:</zparagraph>
</FONT>
<P>
<A NAME="1008376"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>function person(name, age, sex) {<br>
&nbsp;&nbsp;&nbsp;this.name = name<br>
&nbsp;&nbsp;&nbsp;this.age = age<br>
&nbsp;&nbsp;&nbsp;this.sex = sex<br>
}
</zcodeline>
</FONT>
<P>
<A NAME="1008377"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>and then instantiate two new <FONT FACE="courier, couriernew, monospaced size">person</FONT> objects as follows:</zparagraph>
</FONT>
<P>
<A NAME="1008378"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>rand = new person("Rand McKinnon", 33, "M");<br>
ken = new person("Ken Jones", 39, "M");
</zcodeline>
</FONT>
<P>
<A NAME="1008379"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>Then you can rewrite the definition of <FONT FACE="courier, couriernew, monospaced size">car</FONT> to include an <FONT FACE="courier, couriernew, monospaced size">owner</FONT> property that takes a <FONT FACE="courier, couriernew, monospaced size">person</FONT> object, as follows:</zparagraph>
</FONT>
<P>
<A NAME="1018834"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>function car(make, model, year, owner) {<br>
&nbsp;&nbsp;&nbsp;this.make = make;<br>
&nbsp;&nbsp;&nbsp;this.model = model;<br>
&nbsp;&nbsp;&nbsp;this.year = year;<br>
&nbsp;&nbsp;&nbsp;this.owner = owner<br>
}
</zcodeline>
</FONT>
<P>
<A NAME="1008381"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>To instantiate the new objects, you then use the following:</zparagraph>
</FONT>
<P>
<A NAME="1008382"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>car1 = new car("Eagle", "Talon TSi", 1993, rand);<br>
car2 = new car("Nissan", "300ZX", 1992, ken);
</zcodeline>
</FONT>
<P>
<A NAME="1008383"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>Notice that instead of passing a literal string or integer value when creating the new objects, the above statements pass the objects <FONT FACE="courier, couriernew, monospaced size">rand</FONT> and <FONT FACE="courier, couriernew, monospaced size">ken</FONT> as the arguments for the owners. Then if you want to find out the name of the owner of car2, you can access the following property:</zparagraph>
</FONT>
<P>
<A NAME="1008384"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>car2.owner.name
</zcodeline>
</FONT>
<P>
<A NAME="1008386"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>Note that you can always add a property to a previously defined object. For example, the statement</zparagraph>
</FONT>
<P>
<A NAME="1008387"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>car1.color = "black"
</zcodeline>
</FONT>
<P>
<A NAME="1008388"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>adds a property <FONT FACE="courier, couriernew, monospaced size">color</FONT> to car1, and assigns it a value of "black." However, this does not affect any other objects. To add the new property to all objects of the same type, you have to add the property to the definition of the <FONT FACE="courier, couriernew, monospaced size">car</FONT> object type.</zparagraph>
</FONT>
<P>
<P>
<BR>
<A NAME="1008392"> </A>
<A NAME="Indexing Object Properties"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="4">
<zhead2>Indexing Object Properties</zhead2>
</FONT>
<BR>
<BR>



<A NAME="1008393"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>In JavaScript 1.0, you can refer to an object's properties by their property name or by their ordinal index. In JavaScript 1.1 or later, however, if you initially define a property by its name, you must always refer to it by its name, and if you initially define a property by an index, you must always refer to it by its index.</zparagraph>
</FONT>
<P>

<A NAME="1008394"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>This applies when you create an object and its properties with a constructor function, as in the above example of the <FONT FACE="courier, couriernew, monospaced size">Car</FONT> object type, and when you define individual properties explicitly (for example, <FONT FACE="courier, couriernew, monospaced size">myCar.color = "red"</FONT>). So if you define object properties initially with an index, such as <FONT FACE="courier, couriernew, monospaced size">myCar[5] = "25 mpg"</FONT>, you can subsequently refer to the property as <FONT FACE="courier, couriernew, monospaced size">myCar[5]</FONT>.</zparagraph>
</FONT>
<P>

<A NAME="1008395"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The exception to this rule is objects reflected from HTML, such as the <FONT FACE="courier, couriernew, monospaced size">forms</FONT> array. You can always refer to objects in these arrays by either their ordinal number (based on where they appear in the document) or their name (if defined). For example, if the second <FONT FACE="courier, couriernew, monospaced size">&lt;FORM&gt;</FONT> tag in a document has a <FONT FACE="courier, couriernew, monospaced size">NAME</FONT> attribute of "myForm", you can refer to the form as <FONT FACE="courier, couriernew, monospaced size">document.forms[1]</FONT> or <FONT FACE="courier, couriernew, monospaced size">document.forms["myForm"]</FONT> or <FONT FACE="courier, couriernew, monospaced size">document.myForm</FONT>.</zparagraph>
</FONT>
<P>
<P>
<BR>
<A NAME="1008397"> </A>
<A NAME="Defining Properties for an Object Type"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="4">
<zhead2>Defining Properties for an Object Type</zhead2>
</FONT>
<BR>
<BR>



<A NAME="1008398"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>You can add a property to a previously defined object type by using the <FONT FACE="courier, couriernew, monospaced size">prototype</FONT> property. This defines a property that is shared by all objects of the specified type, rather than by just one instance of the object. The following code adds a <FONT FACE="courier, couriernew, monospaced size">color</FONT> property to all objects of type <FONT FACE="courier, couriernew, monospaced size">car</FONT>, and then assigns a value to the <FONT FACE="courier, couriernew, monospaced size">color</FONT> property of the object <FONT FACE="courier, couriernew, monospaced size">car1</FONT>.</zparagraph>
</FONT>
<P>
<A NAME="1008404"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>Car.prototype.color=null;<br>
car1.color="black";
</zcodeline>
</FONT>
<P>
<A NAME="1009697"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>See the <FONT FACE="courier, couriernew, monospaced size">prototype</FONT> property of the <FONT FACE="courier, couriernew, monospaced size">Function</FONT> object in the <A HREF="http://developer.netscape.com/docs/manuals/js/core/jsref/index.htm"><I>Core JavaScript Reference</I></A> for more information.</zparagraph>
</FONT>
<P>
<P>
<BR>
<A NAME="1008406"> </A>
<A NAME="Defining Methods"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="4">
<zhead2>Defining Methods</zhead2>
</FONT>
<BR>
<BR>



<A NAME="1008407"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>A <I>method</I> is a function associated with an object. You define a method the same way you define a standard function. Then you use the following syntax to associate the function with an existing object:</zparagraph>
</FONT>
<P>
<A NAME="1008408"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>object.methodname = function_name
</zcodeline>
</FONT>
<P>
<A NAME="1008409"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>where <FONT FACE="courier, couriernew, monospaced size">object</FONT> is an existing object, <FONT FACE="courier, couriernew, monospaced size">methodname</FONT> is the name you are assigning to the method, and <FONT FACE="courier, couriernew, monospaced size">function_name</FONT> is the name of the function.</zparagraph>
</FONT>
<P>

<A NAME="1008410"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>You can then call the method in the context of the object as follows:</zparagraph>
</FONT>
<P>
<A NAME="1008411"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>object.methodname(params);
</zcodeline>
</FONT>
<P>
<A NAME="1008412"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>You can define methods for an object type by including a method definition in the object constructor function. For example, you could define a function that would format and display the properties of the previously-defined <FONT FACE="courier, couriernew, monospaced size">car</FONT> objects; for example,</zparagraph>
</FONT>
<P>
<A NAME="1008413"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>function displayCar() {<br>
&nbsp;&nbsp;&nbsp;var result = "A Beautiful " + this.year + " " + this.make <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+ " " + this.model;<br>
&nbsp;&nbsp;&nbsp;pretty_print(result);<br>
}
</zcodeline>
</FONT>
<P>
<A NAME="1008417"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>where <FONT FACE="courier, couriernew, monospaced size">pretty_print</FONT> is function to display a horizontal rule and a string. Notice the use of <FONT FACE="courier, couriernew, monospaced size">this</FONT> to refer to the object to which the method belongs.</zparagraph>
</FONT>
<P>

<A NAME="1008419"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>You can make this function a method of <FONT FACE="courier, couriernew, monospaced size">car</FONT> by adding the statement</zparagraph>
</FONT>
<P>
<A NAME="1008420"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>this.displayCar = displayCar;
</zcodeline>
</FONT>
<P>
<A NAME="1008421"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>to the object definition. So, the full definition of <FONT FACE="courier, couriernew, monospaced size">car</FONT> would now look like</zparagraph>
</FONT>
<P>
<A NAME="1008422"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>function car(make, model, year, owner) {<br>
&nbsp;&nbsp;&nbsp;this.make = make;<br>
&nbsp;&nbsp;&nbsp;this.model = model;<br>
&nbsp;&nbsp;&nbsp;this.year = year;<br>
&nbsp;&nbsp;&nbsp;this.owner = owner;<br>
&nbsp;&nbsp;&nbsp;this.displayCar = displayCar;<br>
}
</zcodeline>
</FONT>
<P>
<A NAME="1008423"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>Then you can call the <FONT FACE="courier, couriernew, monospaced size">displayCar</FONT> method for each of the objects as follows:</zparagraph>
</FONT>
<P>
<A NAME="1008424"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>car1.displayCar()<br>
car2.displayCar()
</zcodeline>
</FONT>
<P>
<A NAME="1008428"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>This produces the output shown in the following figure.</zparagraph>
</FONT>
<P>

<A NAME="1008433"> </A>
<ALIGN="LEFT">
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>
<zfigurecaptionp>Figure 7.1	&nbsp&nbsp Displaying method output <br><img src="graphics/obja.gif">

<br></zfigurecaptionp>
</B>
</FONT>
<P><P>
<BR>
<A NAME="1008434"> </A>
<A NAME="Using this for Object References"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="4">
<zhead2>Using this for Object References</zhead2>
</FONT>
<BR>
<BR>



<A NAME="1008436"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>JavaScript has a special keyword, <FONT FACE="courier, couriernew, monospaced size">this</FONT>, that you can use within a method to refer to the current object. For example, suppose you have a function called <FONT FACE="courier, couriernew, monospaced size">validate</FONT> that validates an object's <FONT FACE="courier, couriernew, monospaced size">value</FONT> property, given the object and the high and low values:</zparagraph>
</FONT>
<P>
<A NAME="1008437"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>function validate(obj, lowval, hival) {<br>
&nbsp;&nbsp;&nbsp;if ((obj.value &lt; lowval) || (obj.value &gt; hival))<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alert("Invalid Value!")<br>
}
</zcodeline>
</FONT>
<P>
<A NAME="1008438"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>Then, you could call <FONT FACE="courier, couriernew, monospaced size">validate</FONT> in each form element's <FONT FACE="courier, couriernew, monospaced size">onChange</FONT> event handler, using <FONT FACE="courier, couriernew, monospaced size">this</FONT> to pass it the form element, as in the following example:</zparagraph>
</FONT>
<P>
<A NAME="1008439"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>&lt;INPUT TYPE="text" NAME="age" SIZE=3 <br>
&nbsp;&nbsp;&nbsp;onChange="validate(this, 18, 99)"&gt;
</zcodeline>
</FONT>
<P>
<A NAME="1008440"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>In general, <FONT FACE="courier, couriernew, monospaced size">this</FONT> refers to the calling object in a method.</zparagraph>
</FONT>
<P>

<A NAME="1008441"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>When combined with the <FONT FACE="courier, couriernew, monospaced size">form</FONT> property, <FONT FACE="courier, couriernew, monospaced size">this</FONT> can refer to the current object's parent form. In the following example, the form <FONT FACE="courier, couriernew, monospaced size">myForm</FONT> contains a <FONT FACE="courier, couriernew, monospaced size">Text</FONT> object and a button. When the user clicks the button, the value of the <FONT FACE="courier, couriernew, monospaced size">Text</FONT> object is set to the form's name. The button's <FONT FACE="courier, couriernew, monospaced size">onClick</FONT> event handler uses <FONT FACE="courier, couriernew, monospaced size">this.form</FONT> to refer to the parent form, <FONT FACE="courier, couriernew, monospaced size">myForm</FONT>.</zparagraph>
</FONT>
<P>
<A NAME="1008442"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>&lt;FORM NAME="myForm"&gt;<br>
Form name:&lt;INPUT TYPE="text" NAME="text1" VALUE="Beluga"&gt;<br>
&lt;P&gt;<br>
&lt;INPUT NAME="button1" TYPE="button" VALUE="Show Form Name"<br>
&nbsp;&nbsp;&nbsp;onClick="this.form.text1.value=this.form.name"&gt;<br>
&lt;/FORM&gt;
</zcodeline>
</FONT>
<P><P>
<BR>
<A NAME="1018325"> </A>
<A NAME="Defining Getters and Setters"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="4">
<zhead2>Defining Getters and Setters</zhead2>
</FONT>
<BR>
<BR>



<A NAME="1018333"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>A getter is a method that gets the value of a specific property. A setter is a method that sets the value of a specific property. You can define getters and setters on any predefined core object or user-defined object that supports the addition of new properties. The syntax for defining getters and setters uses the object literal syntax.</zparagraph>
</FONT>
<P>

<A NAME="1018382"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The following JS shell session illustrates how getters and setters could work for a user-defined object <FONT FACE="courier, couriernew, monospaced size">o</FONT>. The JS shell is an application that allows developers to test JavaScript code in batch mode or interactively. </zparagraph>
</FONT>
<P>

<A NAME="1018849"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The <FONT FACE="courier, couriernew, monospaced size">o</FONT> object's properties are:</zparagraph>
</FONT>
<P>
<ul>
<A NAME="1018428"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1><FONT FACE="courier, couriernew, monospaced size">o.a</FONT> - a number</zsmartlist1>
</FONT>
<P><A NAME="1018429"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1><FONT FACE="courier, couriernew, monospaced size">o.b</FONT> - a getter that returns <FONT FACE="courier, couriernew, monospaced size">o.a</FONT> plus 1</zsmartlist1>
</FONT>
<P><A NAME="1018432"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1><FONT FACE="courier, couriernew, monospaced size">o.c</FONT> - a setter that sets the value of <FONT FACE="courier, couriernew, monospaced size">o.a</FONT> to half of its value</zsmartlist1>
</FONT>
<P><A NAME="1018398"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>js&gt; o = new Object;<br>
[object Object]<br>
js&gt; o = {a:7, get b() {return this.a+1; }, set c(x) {this.a = x/2}};<br>
[object Object]<br>
js&gt; o.a<br>
7<br>
js&gt; o.b<br>
8<br>
js&gt; o.c = 50<br>
js&gt; o.a<br>
25<br>
js&gt;
</zcodeline>
</FONT>
<P></ul>

<A NAME="1018387"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>This JavaScript shell session illustrates how getters and setters can extend the <FONT FACE="courier, couriernew, monospaced size">Date</FONT> prototype to add a year property to all instances of the predefined <FONT FACE="courier, couriernew, monospaced size">Date</FONT> class. It uses the <FONT FACE="courier, couriernew, monospaced size">Date</FONT> class's existing <FONT FACE="courier, couriernew, monospaced size">getFullYear</FONT> and <FONT FACE="courier, couriernew, monospaced size">setFullYear</FONT> methods to support the <FONT FACE="courier, couriernew, monospaced size">year</FONT> property's getter and setter.</zparagraph>
</FONT>
<P>

<A NAME="1018854"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>These statements define a getter and setter for the <FONT FACE="courier, couriernew, monospaced size">year</FONT> property.:</zparagraph>
</FONT>
<P>
<A NAME="1018461"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>js&gt; var d = Date.prototype;<br>
js&gt; d.year getter= function()  { return this.getFullYear(); };
</zcodeline>
</FONT>
<P><A NAME="1018468"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>js&gt; d.year setter= function(y) { return this.setFullYear(y); };
</zcodeline>
</FONT>
<P>
<A NAME="1018445"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>These statements use the getter and setter in a Date object:</zparagraph>
</FONT>
<P>
<A NAME="1018868"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>js&gt; var now = new Date;<br>
js&gt; print(now.year);<br>
2000<br>
js&gt; now.year=2001;<br>
987617605170<br>
js&gt; print(now);<br>
Wed Apr 18 11:13:25 GMT-0700 (Pacific Daylight Time) 2001
</zcodeline>
</FONT>
<P><P>
<BR>
<A NAME="1008445"> </A>
<A NAME="Deleting Properties"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="4">
<zhead2>Deleting Properties</zhead2>
</FONT>
<BR>
<BR>



<A NAME="1016365"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>You can remove a property by using the <FONT FACE="courier, couriernew, monospaced size">delete</FONT> operator. The following code shows how to remove a property.</zparagraph>
</FONT>
<P>

<A NAME="1018887"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph><FONT FACE="courier, couriernew, monospaced size">//Creates a new property, myobj, with two properties, a and b.<br>myobj = new Object;<br>myobj.a=5;<br>myobj.b=12;</FONT></zparagraph>
</FONT>
<P>

<A NAME="1018904"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph><FONT FACE="courier, couriernew, monospaced size">//Removes the a property, leaving myobj with only the b property.<br>delete myobj.a;</FONT></zparagraph>
</FONT>
<P>

<A NAME="1017136"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>You can also use delete to delete a global variable if the <B>var</B> keyword was not used to declare the variable:</zparagraph>
</FONT>
<P>

<A NAME="1018916"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph><FONT FACE="courier, couriernew, monospaced size">g = 17;<br>delete g;</FONT></zparagraph>
</FONT>
<P>

<A NAME="1018915"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>See <A HREF="expr.html#1008702"  >"delete" on page&nbsp;46</A> for more information.</zparagraph>
</FONT>
<P>

<BR>
<BR>

<A NAME="1010352"> </A>
<A NAME="Predefined Core Objects"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="4">
<zhead1>Predefined Core Objects</zhead1>
<HR SIZE="2" NOSHADE>
</FONT>
<BR>


<A NAME="1013801"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>This section describes the predefined objects in core JavaScript: <FONT FACE="courier, couriernew, monospaced size">Array</FONT>, <FONT FACE="courier, couriernew, monospaced size">Boolean</FONT>, <FONT FACE="courier, couriernew, monospaced size">Date</FONT>, <FONT FACE="courier, couriernew, monospaced size">Function</FONT>, <FONT FACE="courier, couriernew, monospaced size">Math</FONT>, <FONT FACE="courier, couriernew, monospaced size">Number</FONT>, <FONT FACE="courier, couriernew, monospaced size">RegExp</FONT>, and <FONT FACE="courier, couriernew, monospaced size">String</FONT>. </zparagraph>
</FONT>
<P>
<P>
<BR>
<A NAME="1008453"> </A>
<A NAME="Array Object"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="4">
<zhead2>Array Object</zhead2>
</FONT>
<BR>
<BR>



<A NAME="1008454"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>JavaScript does not have an explicit array data type. However, you can use the predefined <FONT FACE="courier, couriernew, monospaced size">Array</FONT> object and its methods to work with arrays in your applications. The <FONT FACE="courier, couriernew, monospaced size">Array</FONT> object has methods for manipulating arrays in various ways, such as joining, reversing, and sorting them. It has a property for determining the array length and other properties for use with regular expressions.</zparagraph>
</FONT>
<P>

<A NAME="1016713"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>An <I>array</I> is an ordered set of values that you refer to with a name and an index. For example, you could have an array called <FONT FACE="courier, couriernew, monospaced size">emp</FONT> that contains employees' names indexed by their employee number. So <FONT FACE="courier, couriernew, monospaced size">emp[1]</FONT> would be employee number one, <FONT FACE="courier, couriernew, monospaced size">emp[2]</FONT> employee number two, and so on.</zparagraph>
</FONT>
<P>
<BR>
<P>
<A NAME="1016718"> </A>
<A NAME="Creating an Array"> </A>
<FONT FACE="Arial, Helvetica, sans-serif">
<B>
<zhead3>Creating an Array</zhead3>
</B>
</FONT>
<BR>
<BR>

<A NAME="1016715"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>To create an <FONT FACE="courier, couriernew, monospaced size">Array</FONT> object:</zparagraph>
</FONT>
<P>
<A NAME="1008459"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>1. arrayObjectName = new Array(element0, element1, ..., element<I>N</I>)<br>
2. arrayObjectName = new Array(arrayLength)
</zcodeline>
</FONT>
<P>
<A NAME="1008460"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph><FONT FACE="courier, couriernew, monospaced size">arrayObjectName</FONT> is either the name of a new object or a property of an existing object. When using <FONT FACE="courier, couriernew, monospaced size">Array</FONT> properties and methods, <FONT FACE="courier, couriernew, monospaced size">arrayObjectName</FONT> is either the name of an existing <FONT FACE="courier, couriernew, monospaced size">Array</FONT> object or a property of an existing object.</zparagraph>
</FONT>
<P>

<A NAME="1008461"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph><FONT FACE="courier, couriernew, monospaced size">element0, element1, ..., element</FONT><FONT FACE="courier, couriernew, monospaced size"><I>N</I></font> is a list of values for the array's elements. When this form is specified, the array is initialized with the specified values as its elements, and the array's <FONT FACE="courier, couriernew, monospaced size">length</FONT> property is set to the number of arguments.</zparagraph>
</FONT>
<P>

<A NAME="1009639"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph><FONT FACE="courier, couriernew, monospaced size">arrayLength</FONT> is the initial length of the array. The following code creates an array of five elements:</zparagraph>
</FONT>
<P>
<A NAME="1009640"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>billingMethod = new Array(5)
</zcodeline>
</FONT>
<P>
<A NAME="1016735"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>Array literals are also <FONT FACE="courier, couriernew, monospaced size">Array</FONT> objects; for example, the following literal is an <FONT FACE="courier, couriernew, monospaced size">Array</FONT> object. See <A HREF="ident.html#1011655"  >"Array Literals" on page&nbsp;28</A> for details on array literals.</zparagraph>
</FONT>
<P>
<A NAME="1016774"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>coffees = ["French Roast", "Columbian", "Kona"]
</zcodeline>
</FONT>
<P><BR>
<P>
<A NAME="1016745"> </A>
<A NAME="Populating an Array"> </A>
<FONT FACE="Arial, Helvetica, sans-serif">
<B>
<zhead3>Populating an Array</zhead3>
</B>
</FONT>
<BR>
<BR>

<A NAME="1016746"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>You can populate an array by assigning values to its elements. For example,</zparagraph>
</FONT>
<P>
<A NAME="1016747"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>emp[1] = "Casey Jones"<br>
emp[2] = "Phil Lesh"<br>
emp[3] = "August West"
</zcodeline>
</FONT>
<P>
<A NAME="1016748"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>You can also populate an array when you create it:</zparagraph>
</FONT>
<P>
<A NAME="1016749"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>myArray = new Array("Hello", myVar, 3.14159)
</zcodeline>
</FONT>
<P><BR>
<P>
<A NAME="1016756"> </A>
<A NAME="Referring to Array Elements"> </A>
<FONT FACE="Arial, Helvetica, sans-serif">
<B>
<zhead3>Referring to Array Elements</zhead3>
</B>
</FONT>
<BR>
<BR>

<A NAME="1016757"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>You refer to an array's elements by using the element's ordinal number. For example, suppose you define the following array:</zparagraph>
</FONT>
<P>
<A NAME="1016758"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>myArray = new Array("Wind","Rain","Fire")
</zcodeline>
</FONT>
<P>
<A NAME="1016759"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>You then refer to the first element of the array as <FONT FACE="courier, couriernew, monospaced size">myArray[0]</FONT> and the second element of the array as <FONT FACE="courier, couriernew, monospaced size">myArray[1]</FONT>.</zparagraph>
</FONT>
<P>

<A NAME="1016760"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The index of the elements begins with zero (0), but the length of array (for example, <FONT FACE="courier, couriernew, monospaced size">myArray.length</FONT>) reflects the number of elements in the array.</zparagraph>
</FONT>
<P>
<BR>
<P>
<A NAME="1016725"> </A>
<A NAME="Array Methods"> </A>
<FONT FACE="Arial, Helvetica, sans-serif">
<B>
<zhead3>Array Methods</zhead3>
</B>
</FONT>
<BR>
<BR>

<A NAME="1008467"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The <FONT FACE="courier, couriernew, monospaced size">Array</FONT> object has the following methods:</zparagraph>
</FONT>
<P>
<ul>
<A NAME="1008468"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1><FONT FACE="courier, couriernew, monospaced size">concat</FONT> joins two arrays and returns a new array. </zsmartlist1>
</FONT>
<P><A NAME="1008469"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1><FONT FACE="courier, couriernew, monospaced size">join</FONT> joins all elements of an array into a string.</zsmartlist1>
</FONT>
<P><A NAME="1008470"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1><FONT FACE="courier, couriernew, monospaced size">pop</FONT> removes the last element from an array and returns that element.</zsmartlist1>
</FONT>
<P><A NAME="1008471"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1><FONT FACE="courier, couriernew, monospaced size">push</FONT> adds one or more elements to the end of an array and returns that last element added.</zsmartlist1>
</FONT>
<P><A NAME="1008472"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1><FONT FACE="courier, couriernew, monospaced size">reverse</FONT> transposes the elements of an array: the first array element becomes the last and the last becomes the first.</zsmartlist1>
</FONT>
<P><A NAME="1008473"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1><FONT FACE="courier, couriernew, monospaced size">shift</FONT> removes the first element from an array and returns that element</zsmartlist1>
</FONT>
<P><A NAME="1008474"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1><FONT FACE="courier, couriernew, monospaced size">slice</FONT> extracts a section of an array and returns a new array.</zsmartlist1>
</FONT>
<P><A NAME="1008475"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1><FONT FACE="courier, couriernew, monospaced size">splice</FONT> adds and/or removes elements from an array.</zsmartlist1>
</FONT>
<P><A NAME="1008476"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1><FONT FACE="courier, couriernew, monospaced size">sort</FONT> sorts the elements of an array.</zsmartlist1>
</FONT>
<P><A NAME="1008477"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1><FONT FACE="courier, couriernew, monospaced size">unshift</FONT> adds one or more elements to the front of an array and returns the new length of the array.</zsmartlist1>
</FONT>
<P></ul>

<A NAME="1008478"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>For example, suppose you define the following array:</zparagraph>
</FONT>
<P>
<A NAME="1008479"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>myArray = new Array("Wind","Rain","Fire")
</zcodeline>
</FONT>
<P>
<A NAME="1008480"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph><FONT FACE="courier, couriernew, monospaced size">myArray.join()</FONT> returns "Wind,Rain,Fire"; <FONT FACE="courier, couriernew, monospaced size">myArray.reverse</FONT> transposes the array so that <FONT FACE="courier, couriernew, monospaced size">myArray[0]</FONT> is "Fire", <FONT FACE="courier, couriernew, monospaced size">myArray[1]</FONT> is "Rain", and <FONT FACE="courier, couriernew, monospaced size">myArray[2]</FONT> is "Wind". <FONT FACE="courier, couriernew, monospaced size">myArray.sort</FONT> sorts the array so that <FONT FACE="courier, couriernew, monospaced size">myArray[0]</FONT> is "Fire", <FONT FACE="courier, couriernew, monospaced size">myArray[1]</FONT> is "Rain", and <FONT FACE="courier, couriernew, monospaced size">myArray[2]</FONT> is "Wind".</zparagraph>
</FONT>
<P>
<BR>
<P>
<A NAME="1009661"> </A>
<A NAME="Two-Dimensional Arrays"> </A>
<FONT FACE="Arial, Helvetica, sans-serif">
<B>
<zhead3>Two-Dimensional Arrays</zhead3>
</B>
</FONT>
<BR>
<BR>

<A NAME="1009662"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The following code creates a two-dimensional array.</zparagraph>
</FONT>
<P>
<A NAME="1008488"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>a = new Array(4)<br>
for (i=0; i &lt; 4; i++) {<br>
&nbsp;&nbsp;&nbsp;a[i] = new Array(4)<br>
&nbsp;&nbsp;&nbsp;for (j=0; j &lt; 4; j++) {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a[i][j] = "["+i+","+j+"]"<br>
&nbsp;&nbsp;&nbsp;}<br>
}
</zcodeline>
</FONT>
<P>
<A NAME="1016808"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>This example creates an array with the following rows:</zparagraph>
</FONT>
<P>
<A NAME="1016809"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>Row 0:[0,0][0,1][0,2][0,3]<br>
Row 1:[1,0][1,1][1,2][1,3]<br>
Row 2:[2,0][2,1][2,2][2,3]<br>
Row 3:[3,0][3,1][3,2][3,3]
</zcodeline>
</FONT>
<P><BR>
<P>
<A NAME="1008496"> </A>
<A NAME="Arrays and Regular Expressions"> </A>
<FONT FACE="Arial, Helvetica, sans-serif">
<B>
<zhead3>Arrays and Regular Expressions</zhead3>
</B>
</FONT>
<BR>
<BR>

<A NAME="1008497"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>When an array is the result of a match between a regular expression and a string, the array returns properties and elements that provide information about the match. An array is the return value of <FONT FACE="courier, couriernew, monospaced size">RegExp.exec</FONT>, <FONT FACE="courier, couriernew, monospaced size">String.match</FONT>, and <FONT FACE="courier, couriernew, monospaced size">String.split</FONT>. For information on using arrays with regular expressions, see <A HREF="regexp.html#1010922"  >Chapter 4, "Regular Expressions."</A></zparagraph>
</FONT>
<P>
<P>
<BR>
<A NAME="1008502"> </A>
<A NAME="Boolean Object"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="4">
<zhead2>Boolean Object</zhead2>
</FONT>
<BR>
<BR>



<A NAME="1008503"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The <FONT FACE="courier, couriernew, monospaced size">Boolean</FONT> object is a wrapper around the primitive Boolean data type. Use the following syntax to create a <FONT FACE="courier, couriernew, monospaced size">Boolean</FONT> object:</zparagraph>
</FONT>
<P>
<A NAME="1008505"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>booleanObjectName = new Boolean(value)
</zcodeline>
</FONT>
<P>
<A NAME="1016879"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>Do not confuse the primitive Boolean values <FONT FACE="courier, couriernew, monospaced size">true</FONT> and <FONT FACE="courier, couriernew, monospaced size">false</FONT> with the true and false values of the <FONT FACE="courier, couriernew, monospaced size">Boolean</FONT> object. Any object whose value is not <FONT FACE="courier, couriernew, monospaced size">undefined</FONT> , <FONT FACE="courier, couriernew, monospaced size">null</FONT>, 0, <FONT FACE="courier, couriernew, monospaced size">NaN</FONT>, or the empty string , including a <FONT FACE="courier, couriernew, monospaced size">Boolean</FONT> object whose value is false, evaluates to true when passed to a conditional statement. See <A HREF="stmtsov.html#1008323"  >"if...else Statement" on page&nbsp;68</A> for more information.</zparagraph>
</FONT>
<P>
<P>
<BR>
<A NAME="1008512"> </A>
<A NAME="Date Object"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="4">
<zhead2>Date Object</zhead2>
</FONT>
<BR>
<BR>



<A NAME="1008514"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>JavaScript does not have a date data type. However, you can use the <FONT FACE="courier, couriernew, monospaced size">Date</FONT> object and its methods to work with dates and times in your applications. The <FONT FACE="courier, couriernew, monospaced size">Date</FONT> object has a large number of methods for setting, getting, and manipulating dates. It does not have any properties.</zparagraph>
</FONT>
<P>

<A NAME="1008515"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>JavaScript handles dates similarly to Java. The two languages have many of the same date methods, and both languages store dates as the number of milliseconds since January 1, 1970, 00:00:00.</zparagraph>
</FONT>
<P>

<A NAME="1008517"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The <FONT FACE="courier, couriernew, monospaced size">Date</FONT> object range is -100,000,000 days to 100,000,000 days relative to 01 January, 1970 UTC.</zparagraph>
</FONT>
<P>

<A NAME="1008519"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>To create a <FONT FACE="courier, couriernew, monospaced size">Date</FONT> object:</zparagraph>
</FONT>
<P>
<A NAME="1008520"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>dateObjectName = new Date([parameters])
</zcodeline>
</FONT>
<P>
<A NAME="1008521"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>where <FONT FACE="courier, couriernew, monospaced size">dateObjectName</FONT> is the name of the <FONT FACE="courier, couriernew, monospaced size">Date</FONT> object being created; it can be a new object or a property of an existing object.</zparagraph>
</FONT>
<P>

<A NAME="1008522"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The <FONT FACE="courier, couriernew, monospaced size">parameters</FONT> in the preceding syntax can be any of the following:</zparagraph>
</FONT>
<P>
<ul>
<A NAME="1008523"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1>Nothing: creates today's date and time. For example, <FONT FACE="courier, couriernew, monospaced size">today = new Date()</FONT>.</zsmartlist1>
</FONT>
<P><A NAME="1008524"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1>A string representing a date in the following form: "Month day, year hours:minutes:seconds." For example, <FONT FACE="courier, couriernew, monospaced size">Xmas95 = new Date("December 25, 1995 13:30:00")</FONT>. If you omit hours, minutes, or seconds, the value will be set to zero.</zsmartlist1>
</FONT>
<P><A NAME="1008525"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1>A set of integer values for year, month, and day. For example, <FONT FACE="courier, couriernew, monospaced size">Xmas95 = new Date(1995,11,25)</FONT>. A set of values for year, month, day, hour, minute, and seconds. For example, <FONT FACE="courier, couriernew, monospaced size">Xmas95 = new Date(1995,11,25,9,30,0)</FONT>.</zsmartlist1>
</FONT>
<P></ul>
<P>
<A NAME="1017659"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>
<zheadrunin>JavaScript 1.2 and earlier. </zheadrunin>
</B>
</FONT>

<A NAME="1017660"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The <FONT FACE="courier, couriernew, monospaced size">Date</FONT> object behaves as follows:</zparagraph>
</FONT>
<P>
<ul>
<A NAME="1017661"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1>Dates prior to 1970 are not allowed.</zsmartlist1>
</FONT>
<P><A NAME="1017662"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1>JavaScript depends on platform-specific date facilities and behavior; the behavior of the <FONT FACE="courier, couriernew, monospaced size">Date</FONT> object varies from platform to platform.</zsmartlist1>
</FONT>
<P></ul>
<BR>
<P>
<A NAME="1008526"> </A>
<A NAME="Methods of the Date Object"> </A>
<FONT FACE="Arial, Helvetica, sans-serif">
<B>
<zhead3>Methods of the Date Object</zhead3>
</B>
</FONT>
<BR>
<BR>

<A NAME="1008527"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The <FONT FACE="courier, couriernew, monospaced size">Date</FONT> object methods for handling dates and times fall into these broad categories:</zparagraph>
</FONT>
<P>
<ul>
<A NAME="1008528"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1>"set" methods, for setting date and time values in <FONT FACE="courier, couriernew, monospaced size">Date</FONT> objects.</zsmartlist1>
</FONT>
<P><A NAME="1008529"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1>"get" methods, for getting date and time values from <FONT FACE="courier, couriernew, monospaced size">Date</FONT> objects.</zsmartlist1>
</FONT>
<P><A NAME="1008530"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1>"to" methods, for returning string values from <FONT FACE="courier, couriernew, monospaced size">Date</FONT> objects.</zsmartlist1>
</FONT>
<P><A NAME="1008531"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1>parse and UTC methods, for parsing <FONT FACE="courier, couriernew, monospaced size">Date</FONT> strings.</zsmartlist1>
</FONT>
<P></ul>

<A NAME="1008532"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>With the "get" and "set" methods you can get and set seconds, minutes, hours, day of the month, day of the week, months, and years separately. There is a <FONT FACE="courier, couriernew, monospaced size">getDay</FONT> method that returns the day of the week, but no corresponding <FONT FACE="courier, couriernew, monospaced size">setDay</FONT> method, because the day of the week is set automatically. These methods use integers to represent these values as follows:</zparagraph>
</FONT>
<P>
<ul>
<A NAME="1008535"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1>Seconds and minutes: 0 to 59</zsmartlist1>
</FONT>
<P><A NAME="1008536"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1>Hours: 0 to 23</zsmartlist1>
</FONT>
<P><A NAME="1008537"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1>Day: 0 (Sunday) to 6 (Saturday)</zsmartlist1>
</FONT>
<P><A NAME="1008538"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1>Date: 1 to 31 (day of the month)</zsmartlist1>
</FONT>
<P><A NAME="1008539"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1>Months: 0 (January) to 11 (December)</zsmartlist1>
</FONT>
<P><A NAME="1008540"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1>Year: years since 1900</zsmartlist1>
</FONT>
<P></ul>

<A NAME="1008541"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>For example, suppose you define the following date:</zparagraph>
</FONT>
<P>
<A NAME="1008542"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>Xmas95 = new Date("December 25, 1995")
</zcodeline>
</FONT>
<P>
<A NAME="1008543"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>Then <FONT FACE="courier, couriernew, monospaced size">Xmas95.getMonth()</FONT> returns 11, and <FONT FACE="courier, couriernew, monospaced size">Xmas95.getFullYear()</FONT> returns 1995.</zparagraph>
</FONT>
<P>

<A NAME="1008546"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The <FONT FACE="courier, couriernew, monospaced size">getTime</FONT> and <FONT FACE="courier, couriernew, monospaced size">setTime</FONT> methods are useful for comparing dates. The <FONT FACE="courier, couriernew, monospaced size">getTime</FONT> method returns the number of milliseconds since January 1, 1970, 00:00:00 for a <FONT FACE="courier, couriernew, monospaced size">Date</FONT> object.</zparagraph>
</FONT>
<P>

<A NAME="1008547"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>For example, the following code displays the number of days left in the current year:</zparagraph>
</FONT>
<P>
<A NAME="1008548"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>today = new Date()<br>
endYear = new Date(1995,11,31,23,59,59,999) // Set day and month<br>
endYear.setFullYear(today.getFullYear()) // Set year to this year<br>
msPerDay = 24 * 60 * 60 * 1000 // Number of milliseconds per day<br>
daysLeft = (endYear.getTime() - today.getTime()) / msPerDay<br>
daysLeft = Math.round(daysLeft) //returns days left in the year
</zcodeline>
</FONT>
<P>
<A NAME="1008549"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>This example creates a <FONT FACE="courier, couriernew, monospaced size">Date</FONT> object named <FONT FACE="courier, couriernew, monospaced size">today</FONT> that contains today's date. It then creates a <FONT FACE="courier, couriernew, monospaced size">Date</FONT> object named <FONT FACE="courier, couriernew, monospaced size">endYear</FONT> and sets the year to the current year. Then, using the number of milliseconds per day, it computes the number of days between today and <FONT FACE="courier, couriernew, monospaced size">endYear,</FONT> using <FONT FACE="courier, couriernew, monospaced size">getTime</FONT> and rounding to a whole number of days.</zparagraph>
</FONT>
<P>

<A NAME="1008551"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The<FONT FACE="courier, couriernew, monospaced size"> parse</FONT> method is useful for assigning values from date strings to existing <FONT FACE="courier, couriernew, monospaced size">Date</FONT> objects. For example, the following code uses <FONT FACE="courier, couriernew, monospaced size">parse</FONT> and <FONT FACE="courier, couriernew, monospaced size">setTime</FONT> to assign a date value to the <FONT FACE="courier, couriernew, monospaced size">IPOdate</FONT> object:</zparagraph>
</FONT>
<P>
<A NAME="1008553"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>IPOdate = new Date()<br>
IPOdate.setTime(Date.parse("Aug 9, 1995"))
</zcodeline>
</FONT>
<P><BR>
<P>
<A NAME="1008554"> </A>
<A NAME="Using the Date Object: an Example"> </A>
<FONT FACE="Arial, Helvetica, sans-serif">
<B>
<zhead3>Using the Date Object: an Example</zhead3>
</B>
</FONT>
<BR>
<BR>

<A NAME="1016971"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>In the following example, the function <FONT FACE="courier, couriernew, monospaced size">JSClock()</FONT> returns the time in the format of a digital clock.</zparagraph>
</FONT>
<P>
<A NAME="1016947"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>function JSClock() {<br>
&nbsp;&nbsp;&nbsp;var time = new Date()<br>
&nbsp;&nbsp;&nbsp;var hour = time.getHours()<br>
&nbsp;&nbsp;&nbsp;var minute = time.getMinutes()<br>
&nbsp;&nbsp;&nbsp;var second = time.getSeconds()<br>
&nbsp;&nbsp;&nbsp;var temp = "" + ((hour &gt; 12) ? hour - 12 : hour)<br>
&nbsp;&nbsp;&nbsp;if (hour == 0)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;temp = "12";<br>
&nbsp;&nbsp;&nbsp;temp += ((minute &lt; 10) ? ":0" : ":") + minute<br>
&nbsp;&nbsp;&nbsp;temp += ((second &lt; 10) ? ":0" : ":") + second<br>
&nbsp;&nbsp;&nbsp;temp += (hour &gt;= 12) ? " P.M." : " A.M."<br>
&nbsp;&nbsp;&nbsp;return temp<br>
}
</zcodeline>
</FONT>
<P>
<A NAME="1008571"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The <FONT FACE="courier, couriernew, monospaced size">JSClock</FONT> function first creates a new <FONT FACE="courier, couriernew, monospaced size">Date</FONT> object called <FONT FACE="courier, couriernew, monospaced size">time</FONT>; since no arguments are given, time is created with the current date and time. Then calls to the <FONT FACE="courier, couriernew, monospaced size">getHours</FONT>, <FONT FACE="courier, couriernew, monospaced size">getMinutes</FONT>, and <FONT FACE="courier, couriernew, monospaced size">getSeconds </FONT>methods assign the value of the current hour, minute and seconds to <FONT FACE="courier, couriernew, monospaced size">hour</FONT>, <FONT FACE="courier, couriernew, monospaced size">minute</FONT>, and <FONT FACE="courier, couriernew, monospaced size">second</FONT>. </zparagraph>
</FONT>
<P>

<A NAME="1008575"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The next four statements build a string value based on the time. The first statement creates a variable <FONT FACE="courier, couriernew, monospaced size">temp</FONT>, assigning it a value using a conditional expression; if <FONT FACE="courier, couriernew, monospaced size">hour</FONT> is greater than 12, (<FONT FACE="courier, couriernew, monospaced size">hour</FONT> - 12), otherwise simply <FONT FACE="courier, couriernew, monospaced size">hour</FONT>, unless hour is 0, in which case it becomes 12.</zparagraph>
</FONT>
<P>

<A NAME="1008576"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The next statement appends a <FONT FACE="courier, couriernew, monospaced size">minute</FONT> value to <FONT FACE="courier, couriernew, monospaced size">temp</FONT>. If the value of <FONT FACE="courier, couriernew, monospaced size">minute</FONT> is less than 10, the conditional expression adds a string with a preceding zero; otherwise it adds a string with a demarcating colon. Then a statement appends a seconds value to <FONT FACE="courier, couriernew, monospaced size">temp</FONT> in the same way.</zparagraph>
</FONT>
<P>

<A NAME="1008577"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>Finally, a conditional expression appends "PM" to <FONT FACE="courier, couriernew, monospaced size">temp</FONT> if <FONT FACE="courier, couriernew, monospaced size">hour</FONT> is 12 or greater; otherwise, it appends "AM" to <FONT FACE="courier, couriernew, monospaced size">temp</FONT>.</zparagraph>
</FONT>
<P>
<P>
<BR>
<A NAME="1008590"> </A>
<A NAME="Function Object"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="4">
<zhead2>Function Object</zhead2>
</FONT>
<BR>
<BR>



<A NAME="1008591"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The predefined <FONT FACE="courier, couriernew, monospaced size">Function</FONT> object specifies a string of JavaScript code to be compiled as a function.</zparagraph>
</FONT>
<P>

<A NAME="1008592"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>To create a <FONT FACE="courier, couriernew, monospaced size">Function</FONT> object:</zparagraph>
</FONT>
<P>
<A NAME="1008593"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>functionObjectName = new Function ([arg1, arg2, ... argn], functionBody)
</zcodeline>
</FONT>
<P>
<A NAME="1008594"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph><FONT FACE="courier, couriernew, monospaced size">functionObjectName </FONT>is the name of a variable or a property of an existing object. It can also be an object followed by a lowercase event handler name, such as <FONT FACE="courier, couriernew, monospaced size">window.onerror</FONT>. </zparagraph>
</FONT>
<P>

<A NAME="1008595"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph><FONT FACE="courier, couriernew, monospaced size">arg1, arg2, ... argn</FONT> are arguments to be used by the function as formal argument names. Each must be a string that corresponds to a valid JavaScript identifier; for example "x" or "theForm".</zparagraph>
</FONT>
<P>

<A NAME="1008596"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph><FONT FACE="courier, couriernew, monospaced size">functionBody</FONT> is a string specifying the JavaScript code to be compiled as the function body.</zparagraph>
</FONT>
<P>

<A NAME="1008597"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph><FONT FACE="courier, couriernew, monospaced size">Function</FONT> objects are evaluated each time they are used. This is less efficient than declaring a function and calling it within your code, because declared functions are compiled.</zparagraph>
</FONT>
<P>

<A NAME="1008598"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>In addition to defining functions as described here, you can also use the <FONT FACE="courier, couriernew, monospaced size">function</FONT> statement and the function expression. See the <A HREF="http://developer.netscape.com/docs/manuals/js/core/jsref/index.htm"><I>Core JavaScript Reference</I></A> for more information.</zparagraph>
</FONT>
<P>

<A NAME="1008604"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The following code assigns a function to the variable <FONT FACE="courier, couriernew, monospaced size">setBGColor</FONT>. This function sets the current document's background color.</zparagraph>
</FONT>
<P>
<A NAME="1008605"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>var setBGColor = new Function("document.bgColor='antiquewhite'")
</zcodeline>
</FONT>
<P>
<A NAME="1008606"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>To call the <FONT FACE="courier, couriernew, monospaced size">Function</FONT> object, you can specify the variable name as if it were a function. The following code executes the function specified by the <FONT FACE="courier, couriernew, monospaced size">setBGColor</FONT> variable:</zparagraph>
</FONT>
<P>
<A NAME="1008607"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>var colorChoice="antiquewhite"<br>
if (colorChoice=="antiquewhite") {setBGColor()}
</zcodeline>
</FONT>
<P>
<A NAME="1008608"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>You can assign the function to an event handler in either of the following ways:</zparagraph>
</FONT>
<P>
<A NAME="1008609"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>1. document.form1.colorButton.onclick=setBGColor<br>
2. &lt;INPUT NAME="colorButton" TYPE="button"<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VALUE="Change background color"<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;onClick="setBGColor()"&gt;
</zcodeline>
</FONT>
<P>
<A NAME="1008610"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>Creating the variable <FONT FACE="courier, couriernew, monospaced size">setBGColor</FONT> shown above is similar to declaring the following function:</zparagraph>
</FONT>
<P>
<A NAME="1008611"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>function setBGColor() {<br>
&nbsp;&nbsp;&nbsp;document.bgColor='antiquewhite'<br>
}
</zcodeline>
</FONT>
<P>
<A NAME="1008612"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>Assigning a function to a variable is similar to declaring a function, but there are differences:</zparagraph>
</FONT>
<P>
<ul>
<A NAME="1008613"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1>When you assign a function to a variable using <FONT FACE="courier, couriernew, monospaced size">var setBGColor = new Function("...")</FONT>, <FONT FACE="courier, couriernew, monospaced size">setBGColor</FONT> is a variable for which the current value is a reference to the function created with <FONT FACE="courier, couriernew, monospaced size">new Function()</FONT>.</zsmartlist1>
</FONT>
<P><A NAME="1008614"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1>When you create a function using <FONT FACE="courier, couriernew, monospaced size">function setBGColor() {...}</FONT>, <FONT FACE="courier, couriernew, monospaced size">setBGColor</FONT> is not a variable, it is the name of a function.</zsmartlist1>
</FONT>
<P></ul>

<A NAME="1017163"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph><font face="fontsansserif" SIZE="5" COLOR="red"></font>You can nest a function within a function. The nested (inner) function is private to its containing (outer) function:</zparagraph>
</FONT>
<P>
<ul>
<A NAME="1017164"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1>The inner function can be accessed only from statements in the outer function.</zsmartlist1>
</FONT>
<P><A NAME="1017165"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1>The inner function can use the arguments and variables of the outer function. The outer function cannot use the arguments and variables of the inner function.</zsmartlist1>
</FONT>
<P></ul>
<P>
<BR>
<A NAME="1008620"> </A>
<A NAME="Math Object"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="4">
<zhead2>Math Object</zhead2>
</FONT>
<BR>
<BR>



<A NAME="1008621"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The predefined <FONT FACE="courier, couriernew, monospaced size">Math</FONT> object has properties and methods for mathematical constants and functions. For example, the <FONT FACE="courier, couriernew, monospaced size">Math</FONT> object's <FONT FACE="courier, couriernew, monospaced size">PI</FONT> property has the value of pi (3.141...), which you would use in an application as</zparagraph>
</FONT>
<P>
<A NAME="1008623"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>Math.PI
</zcodeline>
</FONT>
<P>
<A NAME="1008624"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>Similarly, standard mathematical functions are methods of <FONT FACE="courier, couriernew, monospaced size">Math</FONT>. These include trigonometric, logarithmic, exponential, and other functions. For example, if you want to use the trigonometric function sine, you would write</zparagraph>
</FONT>
<P>
<A NAME="1008625"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>Math.sin(1.56)
</zcodeline>
</FONT>
<P>
<A NAME="1008626"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>Note that all trigonometric methods of <FONT FACE="courier, couriernew, monospaced size">Math</FONT> take arguments in radians.</zparagraph>
</FONT>
<P>

<A NAME="1008680"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The following table summarizes the <FONT FACE="courier, couriernew, monospaced size">Math</FONT> object's methods.</zparagraph>
</FONT>
<P>

<P>
<A NAME="1010009"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<BR>
<P>
<TABLE WIDTH="90%" BORDER="1" CELLPADDING="5" CELLSPACING="0">
  <CAPTION></CAPTION><P><FONT FACE=""><B>
<A NAME="1008633"> </A>
<A NAME="Methods of Math"> </A>
<ALIGN="LEFT">
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>
<ztablecaption>Table 7.1&#32;&nbsp&nbsp Methods of Math</ztablecaption>
</B>
</FONT>
</B></FONT>
<BR>
<BR>
  <TR BGCOLOR="#CCCCCC" ALIGN=LEFT VALIGN=TOP>
    <TH VALIGN=TOP ALIGN=LEFT><A NAME="1008637"> </A>
<P ALIGN="LEFT">
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>
<ztablehead>Method</ztablehead>
</B>
</FONT>
</TH>
    <TH VALIGN=TOP ALIGN=LEFT><A NAME="1008639"> </A>
<P ALIGN="LEFT">
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>
<ztablehead>Description</ztablehead>
</B>
</FONT>
</TH>
  </DIV></TR>
  <TR>
    <TD VALIGN=TOP><A NAME="1008641"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>abs
</zcodeline>
</FONT>
<P>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1008643"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>Absolute value</ztabletext>
</FONT>&nbsp;</TD>
  </TR>
  <TR>
    <TD VALIGN=TOP><A NAME="1008645"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>sin, cos, tan
</zcodeline>
</FONT>
<P>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1008647"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>Standard trigonometric functions; argument in radians</ztabletext>
</FONT>&nbsp;</TD>
  </TR>
  <TR>
    <TD VALIGN=TOP><A NAME="1008649"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>acos, asin, 
atan, atan2
</zcodeline>
</FONT>
<P>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1008651"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>Inverse trigonometric functions; return values in radians</ztabletext>
</FONT>&nbsp;</TD>
  </TR>
  <TR>
    <TD VALIGN=TOP><A NAME="1008653"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>exp, log
</zcodeline>
</FONT>
<P>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1008655"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>Exponential and natural logarithm, base <FONT FACE="courier, couriernew, monospaced size">e</FONT></ztabletext>
</FONT>&nbsp;</TD>
  </TR>
  <TR>
    <TD VALIGN=TOP><A NAME="1008657"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>ceil
</zcodeline>
</FONT>
<P>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1008659"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>Returns least integer greater than or equal to argument</ztabletext>
</FONT>&nbsp;</TD>
  </TR>
  <TR>
    <TD VALIGN=TOP><A NAME="1008661"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>floor
</zcodeline>
</FONT>
<P>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1008663"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>Returns greatest integer less than or equal to argument</ztabletext>
</FONT>&nbsp;</TD>
  </TR>
  <TR>
    <TD VALIGN=TOP><A NAME="1008665"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>min, max
</zcodeline>
</FONT>
<P>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1008667"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>Returns greater or lesser (respectively) of two arguments</ztabletext>
</FONT>&nbsp;</TD>
  </TR>
  <TR>
    <TD VALIGN=TOP><A NAME="1008669"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>pow
</zcodeline>
</FONT>
<P>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1008671"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>Exponential; first argument is base, second is exponent</ztabletext>
</FONT>&nbsp;</TD>
  </TR>
  <TR>
    <TD VALIGN=TOP><A NAME="1018980"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>random
</zcodeline>
</FONT>
<P>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1018982"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>Returns a random number between 0 and 1.</ztabletext>
</FONT>&nbsp;</TD>
  </TR>
  <TR>
    <TD VALIGN=TOP><A NAME="1008673"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>round
</zcodeline>
</FONT>
<P>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1008675"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>Rounds argument to nearest integer</ztabletext>
</FONT>&nbsp;</TD>
  </TR>
  <TR>
    <TD VALIGN=TOP><A NAME="1008677"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>sqrt
</zcodeline>
</FONT>
<P>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1008679"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>Square root</ztabletext>
</FONT>&nbsp;</TD>
  </TR>
</TABLE>



<P>
</FONT>
<P>


<A NAME="1008681"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>Unlike many other objects, you never create a <FONT FACE="courier, couriernew, monospaced size">Math</FONT> object of your own. You always use the predefined <FONT FACE="courier, couriernew, monospaced size">Math</FONT> object.</zparagraph>
</FONT>
<P>
<P>
<BR>
<A NAME="1008686"> </A>
<A NAME="Number Object"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="4">
<zhead2>Number Object</zhead2>
</FONT>
<BR>
<BR>



<A NAME="1008687"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The <FONT FACE="courier, couriernew, monospaced size">Number</FONT> object has properties for numerical constants, such as maximum value, not-a-number, and infinity. You cannot change the values of these properties and you use them as follows:</zparagraph>
</FONT>
<P>
<A NAME="1008688"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>biggestNum = Number.MAX_VALUE<br>
smallestNum = Number.MIN_VALUE<br>
infiniteNum = Number.POSITIVE_INFINITY<br>
negInfiniteNum = Number.NEGATIVE_INFINITY<br>
notANum = Number.NaN
</zcodeline>
</FONT>
<P>
<A NAME="1008689"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>You always refer to a property of the predefined <FONT FACE="courier, couriernew, monospaced size">Number</FONT> object as shown above, and not as a property of a <FONT FACE="courier, couriernew, monospaced size">Number</FONT> object you create yourself.</zparagraph>
</FONT>
<P>

<A NAME="1008723"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The following table summarizes the <FONT FACE="courier, couriernew, monospaced size">Number</FONT> object's properties.</zparagraph>
</FONT>
<P>

<P>
<A NAME="1018245"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<BR>
<P>
<TABLE WIDTH="90%" BORDER="1" CELLPADDING="5" CELLSPACING="0">
  <CAPTION></CAPTION><P><FONT FACE=""><B>
<A NAME="1018218"> </A>
<A NAME="Properties of Number"> </A>
<ALIGN="LEFT">
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>
<ztablecaption>Table 7.2&#32;&nbsp&nbsp Properties of Number</ztablecaption>
</B>
</FONT>
</B></FONT>
<BR>
<BR>
  <TR BGCOLOR="#CCCCCC" ALIGN=LEFT VALIGN=TOP>
    <TH VALIGN=TOP ALIGN=LEFT><A NAME="1018222"> </A>
<P ALIGN="LEFT">
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>
<ztablehead>Property</ztablehead>
</B>
</FONT>
</TH>
    <TH VALIGN=TOP ALIGN=LEFT><A NAME="1018224"> </A>
<P ALIGN="LEFT">
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>
<ztablehead>Description</ztablehead>
</B>
</FONT>
</TH>
  </DIV></TR>
  <TR>
    <TD VALIGN=TOP><A NAME="1018226"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>MAX_VALUE
</zcodeline>
</FONT>
<P>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1018228"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>The largest representable number</ztabletext>
</FONT>&nbsp;</TD>
  </TR>
  <TR>
    <TD VALIGN=TOP><A NAME="1018230"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>MIN_VALUE
</zcodeline>
</FONT>
<P>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1018232"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>The smallest representable number</ztabletext>
</FONT>&nbsp;</TD>
  </TR>
  <TR>
    <TD VALIGN=TOP><A NAME="1018234"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>NaN
</zcodeline>
</FONT>
<P>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1018236"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>Special "not a number" value</ztabletext>
</FONT>&nbsp;</TD>
  </TR>
  <TR>
    <TD VALIGN=TOP><A NAME="1018238"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>NEGATIVE_INFINITY
</zcodeline>
</FONT>
<P>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1018240"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>Special infinite value; returned on overflow</ztabletext>
</FONT>&nbsp;</TD>
  </TR>
  <TR>
    <TD VALIGN=TOP><A NAME="1018242"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>POSITIVE_INFINITY
</zcodeline>
</FONT>
<P>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1018244"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>Special negative infinite value; returned on overflow</ztabletext>
</FONT>&nbsp;</TD>
  </TR>
</TABLE>



<P>
</FONT>
<P>


<A NAME="1018305"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The Number prototype provides methods for retrieving information from Number objects in various formats. The following table summarizes the methods of <FONT FACE="courier, couriernew, monospaced size">Number.prototype</FONT>.</zparagraph>
</FONT>
<P>

<P>
<A NAME="1018308"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<BR>
<P>
<TABLE WIDTH="90%" BORDER="1" CELLPADDING="5" CELLSPACING="0">
  <CAPTION></CAPTION><P><FONT FACE=""><B>
<A NAME="1018310"> </A>
<A NAME="Methods of Number.prototype"> </A>
<ALIGN="LEFT">
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>
<ztablecaption>Table 7.3&#32;&nbsp&nbsp Methods of Number.prototype</ztablecaption>
</B>
</FONT>
</B></FONT>
<BR>
<BR>
  <TR BGCOLOR="#CCCCCC" ALIGN=LEFT VALIGN=TOP>
    <TH VALIGN=TOP ALIGN=LEFT><A NAME="1018257"> </A>
<P ALIGN="LEFT">
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>
<ztablehead>Method</ztablehead>
</B>
</FONT>
</TH>
    <TH VALIGN=TOP ALIGN=LEFT><A NAME="1018259"> </A>
<P ALIGN="LEFT">
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>
<ztablehead>Description</ztablehead>
</B>
</FONT>
</TH>
  </DIV></TR>
  <TR>
    <TD VALIGN=TOP><A NAME="1018261"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>toExponential
</zcodeline>
</FONT>
<P>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1018263"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>Returns a string representing the number in exponential notation.</ztabletext>
</FONT>&nbsp;</TD>
  </TR>
  <TR>
    <TD VALIGN=TOP><A NAME="1018268"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline><FONT FACE="courier, couriernew, monospaced size">toFixed
</FONT></zcodeline>
</FONT>
<P>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1018270"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>Returns a string representing the number in fixed-point notation.</ztabletext>
</FONT>&nbsp;</TD>
  </TR>
  <TR>
    <TD VALIGN=TOP><A NAME="1018272"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>toPrecision
</zcodeline>
</FONT>
<P>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1018274"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>Returns a string representing the number to a specified precision in fixed-point notation.</ztabletext>
</FONT>&nbsp;</TD>
  </TR>
  <TR>
    <TD VALIGN=TOP><A NAME="1018279"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline><FONT FACE="courier, couriernew, monospaced size">toSource
</FONT></zcodeline>
</FONT>
<P>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1018281"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>Returns an object literal representing the specified Number object; you can use this value to create a new object. Overrides the <A HREF="/Y:/Swap/alanm/jsref1.5/fm/object.html#23401"  ><FONT FACE="courier, couriernew, monospaced size">Object.toSource</FONT></A> method.</ztabletext>
</FONT>&nbsp;</TD>
  </TR>
  <TR>
    <TD VALIGN=TOP><A NAME="1018289"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline><FONT FACE="courier, couriernew, monospaced size">toString
</FONT></zcodeline>
</FONT>
<P>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1018291"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>Returns a string representing the specified object. Overrides the <A HREF="/Y:/Swap/alanm/jsref1.5/fm/object.html#23793"  ><FONT FACE="courier, couriernew, monospaced size">Object.toString</FONT></A> method.</ztabletext>
</FONT>&nbsp;</TD>
  </TR>
  <TR>
    <TD VALIGN=TOP><A NAME="1018299"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline><FONT FACE="courier, couriernew, monospaced size">valueOf
</FONT></zcodeline>
</FONT>
<P>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1018301"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>Returns the primitive value of the specified object. Overrides the <A HREF="/Y:/Swap/alanm/jsref1.5/fm/object.html#39400"  ><FONT FACE="courier, couriernew, monospaced size">Object.valueOf</FONT></A> method.</ztabletext>
</FONT>&nbsp;</TD>
  </TR>
</TABLE>



<P>
</FONT>
<P>

<P>
<BR>
<A NAME="1018247"> </A>
<A NAME="RegExp Object"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="4">
<zhead2>RegExp Object</zhead2>
</FONT>
<BR>
<BR>



<A NAME="1008725"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The <FONT FACE="courier, couriernew, monospaced size">RegExp</FONT> object lets you work with regular expressions. It is described in <A HREF="regexp.html#1010922"  >Chapter 4, "Regular Expressions."</A></zparagraph>
</FONT>
<P>
<P>
<BR>
<A NAME="1008731"> </A>
<A NAME="String Object"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="4">
<zhead2>String Object</zhead2>
</FONT>
<BR>
<BR>



<A NAME="1008732"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The <FONT FACE="courier, couriernew, monospaced size">String</FONT> object is a wrapper around the string primitive data type. Do not confuse a string literal with the <FONT FACE="courier, couriernew, monospaced size">String</FONT> object. For example, the following code creates the string literal <FONT FACE="courier, couriernew, monospaced size">s1</FONT> and also the <FONT FACE="courier, couriernew, monospaced size">String</FONT> object <FONT FACE="courier, couriernew, monospaced size">s2</FONT>:</zparagraph>
</FONT>
<P>
<A NAME="1016978"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>s1 = "foo" //creates a string literal value<br>
s2 = new String("foo") //creates a String object
</zcodeline>
</FONT>
<P>
<A NAME="1016977"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>You can call any of the methods of the <FONT FACE="courier, couriernew, monospaced size">String</FONT> object on a string literal value&#151;JavaScript automatically converts the string literal to a temporary <FONT FACE="courier, couriernew, monospaced size">String</FONT> object, calls the method, then discards the temporary <FONT FACE="courier, couriernew, monospaced size">String</FONT> object. You can also use the <FONT FACE="courier, couriernew, monospaced size">String.length</FONT> property with a string literal.</zparagraph>
</FONT>
<P>

<A NAME="1017005"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>You should use string literals unless you specifically need to use a <FONT FACE="courier, couriernew, monospaced size">String</FONT> object, because <FONT FACE="courier, couriernew, monospaced size">String</FONT> objects can have counterintuitive behavior. For example:</zparagraph>
</FONT>
<P>
<A NAME="1017017"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>s1 = "2 + 2" //creates a string literal value<br>
s2 = new String("2 + 2")//creates a String object<br>
eval(s1) //returns the number 4<br>
eval(s2) //returns the string "2 + 2"
</zcodeline>
</FONT>
<P>
<A NAME="1017046"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>A <FONT FACE="courier, couriernew, monospaced size">String</FONT> object has one property, <FONT FACE="courier, couriernew, monospaced size">length</FONT>, that indicates the number of characters in the string. For example, the following code assigns <FONT FACE="courier, couriernew, monospaced size">x</FONT> the value 13, because "Hello, World!" has 13 characters:</zparagraph>
</FONT>
<P>
<A NAME="1008742"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>myString = "Hello, World!"<br>
x = mystring.length
</zcodeline>
</FONT>
<P>
<A NAME="1008744"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>A <FONT FACE="courier, couriernew, monospaced size">String</FONT> object has two types of methods: those that return a variation on the string itself, such as <FONT FACE="courier, couriernew, monospaced size">substring</FONT> and <FONT FACE="courier, couriernew, monospaced size">toUpperCase</FONT>, and those that return an HTML-formatted version of the string, such as <FONT FACE="courier, couriernew, monospaced size">bold</FONT> and <FONT FACE="courier, couriernew, monospaced size">link</FONT>.</zparagraph>
</FONT>
<P>

<A NAME="1008745"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>For example, using the previous example, both <FONT FACE="courier, couriernew, monospaced size">mystring.toUpperCase()</FONT> and <FONT FACE="courier, couriernew, monospaced size">"hello, world!".toUpperCase()</FONT> return the string "HELLO, WORLD!"</zparagraph>
</FONT>
<P>

<A NAME="1008746"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The <FONT FACE="courier, couriernew, monospaced size">substring</FONT> method takes two arguments and returns a subset of the string between the two arguments. Using the previous example, <FONT FACE="courier, couriernew, monospaced size">mystring.substring(4, 9)</FONT> returns the string "o, Wo". See the <FONT FACE="courier, couriernew, monospaced size">substring</FONT> method of the <FONT FACE="courier, couriernew, monospaced size">String</FONT> object in the <A HREF="http://developer.netscape.com/docs/manuals/js/core/jsref/index.htm"><I>Core JavaScript Reference</I></A> for more information.</zparagraph>
</FONT>
<P>

<A NAME="1008752"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The <FONT FACE="courier, couriernew, monospaced size">String</FONT> object also has a number of methods for automatic HTML formatting, such as <FONT FACE="courier, couriernew, monospaced size">bold</FONT> to create boldface text and <FONT FACE="courier, couriernew, monospaced size">link</FONT> to create a hyperlink. For example, you could create a hyperlink to a hypothetical URL with the <FONT FACE="courier, couriernew, monospaced size">link</FONT> method as follows:</zparagraph>
</FONT>
<P>
<A NAME="1008753"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>mystring.link("http://www.helloworld.com")
</zcodeline>
</FONT>
<P>
<A NAME="1008815"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The following table summarizes the methods of <FONT FACE="courier, couriernew, monospaced size">String</FONT> objects.</zparagraph>
</FONT>
<P>

<P>
<A NAME="1010032"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<BR>
<P>
<TABLE WIDTH="90%" BORDER="1" CELLPADDING="5" CELLSPACING="0">
  <CAPTION></CAPTION><P><FONT FACE=""><B>
<A NAME="1008760"> </A>
<A NAME="Methods of String Instances"> </A>
<ALIGN="LEFT">
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>
<ztablecaption>Table 7.4&#32;&nbsp&nbsp Methods of String Instances</ztablecaption>
</B>
</FONT>
</B></FONT>
<BR>
<BR>
  <TR BGCOLOR="#CCCCCC" ALIGN=LEFT VALIGN=TOP>
    <TH VALIGN=TOP ALIGN=LEFT><A NAME="1008764"> </A>
<P ALIGN="LEFT">
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>
<ztablehead>Method</ztablehead>
</B>
</FONT>
</TH>
    <TH VALIGN=TOP ALIGN=LEFT><A NAME="1008766"> </A>
<P ALIGN="LEFT">
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>
<ztablehead>Description</ztablehead>
</B>
</FONT>
</TH>
  </DIV></TR>
  <TR>
    <TD VALIGN=TOP><A NAME="1008768"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>anchor
</zcodeline>
</FONT>
<P>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1008770"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>Creates HTML named anchor.</ztabletext>
</FONT>&nbsp;</TD>
  </TR>
  <TR>
    <TD VALIGN=TOP><A NAME="1008772"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>big, blink, bold,<br>
fixed, italics, small,<br>
strike, sub, sup
</zcodeline>
</FONT>
<P>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1008774"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>Create HTML formatted string.</ztabletext>
</FONT>&nbsp;</TD>
  </TR>
  <TR>
    <TD VALIGN=TOP><A NAME="1008776"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>charAt, charCodeAt
</zcodeline>
</FONT>
<P>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1008778"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>Return the character or character code at the specified position in string.</ztabletext>
</FONT>&nbsp;</TD>
  </TR>
  <TR>
    <TD VALIGN=TOP><A NAME="1008780"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>indexOf, lastIndexOf
</zcodeline>
</FONT>
<P>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1008782"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>Return the position of specified substring in the string or last position of specified substring, respectively.</ztabletext>
</FONT>&nbsp;</TD>
  </TR>
  <TR>
    <TD VALIGN=TOP><A NAME="1008784"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>link
</zcodeline>
</FONT>
<P>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1008786"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>Creates HTML hyperlink.</ztabletext>
</FONT>&nbsp;</TD>
  </TR>
  <TR>
    <TD VALIGN=TOP><A NAME="1008788"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>concat
</zcodeline>
</FONT>
<P>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1008790"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>Combines the text of two strings and returns a new string.</ztabletext>
</FONT>&nbsp;</TD>
  </TR>
  <TR>
    <TD VALIGN=TOP><A NAME="1008792"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>fromCharCode
</zcodeline>
</FONT>
<P>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1008794"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>Constructs a string from the specified sequence of Unicode values. This is a method of the String class, not a String instance.</ztabletext>
</FONT>&nbsp;</TD>
  </TR>
  <TR>
    <TD VALIGN=TOP><A NAME="1008796"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>split
</zcodeline>
</FONT>
<P>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1008798"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>Splits a <FONT FACE="courier, couriernew, monospaced size">String</FONT> object into an array of strings by separating the string into substrings. </ztabletext>
</FONT>&nbsp;</TD>
  </TR>
  <TR>
    <TD VALIGN=TOP><A NAME="1008800"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>slice
</zcodeline>
</FONT>
<P>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1008802"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>Extracts a section of an string and returns a new string. </ztabletext>
</FONT>&nbsp;</TD>
  </TR>
  <TR>
    <TD VALIGN=TOP><A NAME="1008804"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>substring, substr
</zcodeline>
</FONT>
<P>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1008806"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>Return the specified subset of the string, either by specifying the start and end indexes or the start index and a length.</ztabletext>
</FONT>&nbsp;</TD>
  </TR>
  <TR>
    <TD VALIGN=TOP><A NAME="1008808"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>match, replace, search
</zcodeline>
</FONT>
<P>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1008810"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>Work with regular expressions.</ztabletext>
</FONT>&nbsp;</TD>
  </TR>
  <TR>
    <TD VALIGN=TOP><A NAME="1008812"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>toLowerCase, 
toUpperCase
</zcodeline>
</FONT>
<P>&nbsp;</TD>
    <TD VALIGN=TOP>
<P>
<A NAME="1008814"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<ztabletext>Return the string in all lowercase or all uppercase, respectively.</ztabletext>
</FONT>&nbsp;</TD>
  </TR>
</TABLE>



<P>
</FONT>
<P>




   </BLOCKQUOTE>
</BOOKCONTENT>

<FOOTERCONTENT>
   <BLOCKQUOTE>
 <BR>
            <b><a href="fcns.html">
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2" COLOR="#993300">Previous</a></FONT>&nbsp;&nbsp;&nbsp;&nbsp;</b>
<a href="contents.html">
<b><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2" COLOR="#993300">Contents</a></FONT></b>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="ix.html"><b><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2" COLOR="#993300">Index</a></FONT></b>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="obj2.html">
<b>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2" COLOR="#993300">
Next
</a>
</FONT>
</b>&nbsp;&nbsp;&nbsp;&nbsp;
        <HR NOSHADE SIZE="1">


   <font face="Arial, Helvetica, sans-serif" SIZE="1">

Copyright &#169; 2000 <a href="http://www.netscape.com">Netscape Communications Corp.</a> All rights reserved.</FONT>
   </BLOCKQUOTE>
   <FONT FACE="Arial, Helvetica, sans-serif" SIZE="1">
   <P ALIGN="RIGHT">Last Updated <B>September 28, 2000</B>
   </FONT>
   <BR>

</FOOTERCONTENT>

</MAINCONTENT>

</BODY>


</HTML>